﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Eniux.Windows.ThreeDEngine.Helper;
using System.Windows.Media;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{
    /// <summary>
    /// 截面沿着指定路径，形成管状体
    /// </summary>
    public class TubeVisual3D : ExtrudedVisual3D
    {
        #region 注册属性

        #region Diameter

        /// <summary>
        /// 直径属性
        /// </summary>
        public static readonly DependencyProperty DiameterProperty = DependencyProperty.Register(
            "Diameter", typeof(double), typeof(TubeVisual3D), new UIPropertyMetadata(1.0, SectionChanged));

        /// <summary>
        /// 获取或设置直径
        /// </summary>
        public double Diameter
        {
            get { return (double)GetValue(DiameterProperty); }
            set { SetValue(DiameterProperty, value); }
        } 
        #endregion

        #region ThetaDiv

        /// <summary>
        ///  ThetaDiv属性.
        /// </summary>
        public static readonly DependencyProperty ThetaDivProperty = DependencyProperty.Register(
            "ThetaDiv", typeof(int), typeof(TubeVisual3D), new UIPropertyMetadata(36, SectionChanged));

        /// <summary>
        /// 获取或设置ThetaDiv
        /// </summary>
        public int ThetaDiv
        {
            get { return (int)GetValue(ThetaDivProperty); }
            set { SetValue(ThetaDivProperty, value); }
        } 

        #endregion

        #endregion

        #region 构造、析构

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public TubeVisual3D()
        {
            this.UpdateSection();
        }

        #endregion


        #region 方法

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        protected static void SectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TubeVisual3D)d).UpdateSection();
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateSection()
        {
            var pc = new PointCollection();
            var circle = MeshBuilder.GetCircle(this.ThetaDiv);
            double r = this.Diameter / 2;
            for (int j = 0; j < this.ThetaDiv; j++)
            {
                pc.Add(new Point(circle[j].X * r, circle[j].Y * r));
            }
            this.Section = pc;
        }

        #endregion
    }
}
